//
//  OrderActivityButton.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/4/10.
//  Copyright (c) 2017 蓝布鲁. All rights reserved.
//

#import "OrderActivityButton.h"

@interface OrderActivityButton()

@property (nonatomic, retain)id target;
@property (nonatomic, assign) SEL action;

@end


@implementation OrderActivityButton

- (instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColor.orangeColor;
        [self setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [self addTarget:self action:@selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
        // 手动改变button disable 效果
        self.adjustsImageWhenDisabled = false;
    }
    return self;
}


- (void)setType:(OrderActivityButtonType)type signinTimes:(NSInteger)times {
    _type = type;
    
    NSString *title;

    switch (type){
        case CancelActivityEnable:
            title = @"取消预约";
            self.enabled = true;
            break;
        case CancelActivityDisable:
            title = @"不可取消";
            self.enabled = false;
            break;
        case OrderActivityEnable:
            title = @"预约活动";
            self.enabled = true;
            break;
        case OrderActivityDisable:
            title = @"不可预约";
            self.enabled = false;
            break;
        case DoingActivityDisable:
            title = @"活动已开始";
            self.enabled = false;
            break;
        case CheckEvaluateEnable:
            title = @"已经给分";
            self.enabled = false;
            break;
        case CheckEvaluateDisable:
            title = @"等待老师给分";
            self.enabled = false;
            break;
        case ActivityMiddleDisable:
            title = @"第一次活动结束，第二次还未开始";
            self.enabled = false;
            break;
        case ActivityFinishedDisable:
            title = @"活动已结束";
            self.enabled = false;
            break;
        case ActivityHadSignInDisable:
            title = @"已经签到";
            self.enabled = false;
            break;
        case SignInActivityEnable:
            title = @"已经开始，扫码进行签到";
            self.enabled = false;
            break;
    }
    
    switch (type) {
        case ActivityHadSignInDisable:
        case SignInActivityEnable:
        case CheckEvaluateDisable:
        case CheckEvaluateEnable:
        case DoingActivityDisable:
        case ActivityFinishedDisable:
        case ActivityMiddleDisable:
            if (times) {
                title = [NSString stringWithFormat:@"%@(未签到)",title];
            }else {
                title = [NSString stringWithFormat:@"%@(签到%ld次)",title,times];
            }
            break;
        default:
            break;
    }
    [self setTitle:title forState:UIControlStateNormal];
}

- (void)tappedWithTarget:(id)target action:(SEL)action {
    self.target = target;
    self.action = action;
}

// 点击事件
-(void)buttonTapped {
    if([self.target respondsToSelector:self.action]) {
        [self.target performSelector:self.action];
    }
}

- (void)setEnabled:(BOOL)enabled {
    [super setEnabled:enabled];
    if(enabled == false){
        self.backgroundColor = [UIColor grayColor];
    } else {
        self.backgroundColor = [UIColor orangeColor];
    }
}

@end
